package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baor.dao.ParkingDao;
import com.baor.domain.ParkingVo;
import com.baor.service.ParkingService;
import com.baor.utils.AjaxResult;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.baor.utils.DateUtil.now;


@Service
public class ParkingServiceImpl implements ParkingService {

    @Autowired
    private ParkingDao parkingDao;


    @Override
    public AjaxResult<?> carParkList(Map<String,Object> params) {
        int pageIndex = (int) params.get("pageIndex");
        int pageSize = (int) params.get("pageSize");
        params.put("pageIndex",(pageIndex-1)*pageSize);
        Map<String,Object> res = parkingDao.findPageCount(params);
        List<ParkingVo> list = parkingDao.parkingList(params);
        res.put("parkingList",list);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult<?> addParking(Map<String,Object> params) {
//        parkingVo.setParkingCreatetime(now());
        params.put("parkingCreatetime",now());
        String str = (String) params.get("parkingPays");
        if ( str == null || str.equals("") )
            str = "0";
        String[] nums = str.split(",");//(0微信1支付宝2Apple3HUAWEI)
        StringBuilder builder = new StringBuilder();
        for ( String s : nums ){
            if ( s.equals("0") )
                builder.append("微信,");
            else if ( s.equals("1") )
                builder.append("支付宝,");
            else if ( s.equals("2") )
                builder.append("Apple,");
            else if ( s.equals("3") )
                builder.append("HUAWEI,");
        }
        builder.deleteCharAt(builder.length() - 1);
//        parkingVo.setParkingPays(builder.toString());
        params.put("parkingPays",builder.toString());
        int row = parkingDao.addParking(params);
        if(row > 0 ){
            return AjaxResult.success();
        }else{
            return  AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> updateParking(Map<String,Object> params) {
        String str = (String) params.get("parkingPays");
        if ( str == null || str.equals("") )
            str = "0";
        String[] nums = str.split(",");//(0微信1支付宝2Apple3HUAWEI)
        StringBuilder builder = new StringBuilder();
        for ( String s : nums ){
            if ( s.equals("0") )
                builder.append("微信,");
            else if ( s.equals("1") )
                builder.append("支付宝,");
            else if ( s.equals("2") )
                builder.append("Apple,");
            else if ( s.equals("3") )
                builder.append("HUAWEI,");
        }
        builder.deleteCharAt(builder.length() - 1);
        params.put("parkingPays",builder.toString());
        int row = parkingDao.updateParking(params);
        if(row >0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> delete(Integer parkingId) {
        return parkingDao.deleteById(parkingId) > 0 ? AjaxResult.success() : AjaxResult.error("删除失败") ;
    }

    @Override
    public AjaxResult<?> parkingListInfo(Map<String,Object> params) {
        return AjaxResult.success(parkingDao.parkingList(params));
    }


}
